
<%
	// Retrieve session variables.
	String username = request.getParameter("username");
	String password = request.getParameter("password");
	String retype_password = request.getParameter("retype_password");
	String email = request.getParameter("email");

	// Check if the username is at least 5 characters long and less than
	// or equal 16 characters.
	boolean username_test = username.length() >= 5 && username.length() <= 16;
	// Check if the password is at least 5 characters long and less than
	// or equal 16 characters.
	boolean password_test = password.length() >= 5 && password.length() <= 16;
	// Check if the password and retype password match.
	boolean retype_password_test = password.equals(retype_password);
	// Check if email has an @ character and if there is a period anywhere
	// after the @ character.
	boolean email_test = email.contains("@") && email.indexOf("@") < email.lastIndexOf(".");

	// Compile the error messages together.
	String errors = "";
	if (!username_test) {
		errors += "* Username must be between 5 and 16 characters, inclusive.<br />";
	}
	if (!password_test) {
		errors += "* Password must be between 5 and 16 characters, inclusive.<br />";
	}
	if (!retype_password_test) {
		errors += "* The two passwords do not match.<br />";
	}
	if (!email_test) {
		errors += "* Emails must have the '@' symbol before the last '.'.<br />";
	}

	// If there are any errors abort registration and send the user back to the
	// register page. It also saves non-password type data that the user has submitted.
	if (!errors.equals("")) {
		session.setAttribute("errors", errors);
		session.setAttribute("username", username);
		session.setAttribute("email", email);
		response.sendRedirect("register.jsp");
	} else {
		// Getting information and updating the database.
		java.sql.Connection conn = null;
		try {
			String myJDBCDriver = (String) session.getAttribute("myJDBCDriver");
			String myURL = (String) session.getAttribute("myURL");
			String myUserID = (String) session.getAttribute("myUserID");
			String myPassword = (String) session.getAttribute("myPassword");

			// Connect to the database.
			Class.forName(myJDBCDriver).newInstance();
			java.util.Properties sysprops = System.getProperties();
			sysprops.put("user", myUserID);
			sysprops.put("password", myPassword);
			conn = java.sql.DriverManager.getConnection(myURL, sysprops);

			java.sql.Statement statement = conn.createStatement();
			java.sql.ResultSet rs = statement.executeQuery("SELECT username FROM users WHERE username = '" + username + "'");
			if (rs.next()) {
				// If the username already exists send the user back to the register page with
				// an error message.
				session.setAttribute("errors", "* This username has already been taken.");
				session.setAttribute("username", username);
				session.setAttribute("email", email);
				response.sendRedirect("register.jsp");
			} else {
				// There is no user in the database with the same username.
				username = "'" + username + "'";
				password = "'" + password + "'";
				email = "'" + email + "'";
				statement.executeUpdate("INSERT INTO " + "users (username, password, email, type)" + "VALUES (" + username + "," + password
						+ "," + email + "," + "'student'" + ")");
				response.setHeader("Refresh", "5; login.jsp");
			}
		} catch (Exception e) {
			e.printStackTrace();
			out.print(e.toString());
		} finally {
			try {
				conn.close();
			} catch (Exception ee) {
			}
		}
	}
%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="css/style.css" media="screen" />
<title>Scholar Aide</title>
</head>
<body>
	<jsp:include page="/includes/header.jsp" />
	<div id="wrapper">
		<div id="content">
			<table class="center" id="login">
				<tr>
					<td>Registration Successful! <br />You will be redirected to the login page in 5 seconds. <br /> <b>Note: Edit your profile the first
							time you login for a better experience.</b>
					</td>
				</tr>
			</table>
		</div>
	</div>
	<jsp:include page="/includes/footer.jsp" />
</body>
</html>